package com.ticket.sass.provider.model;


import cn.hutool.core.util.StrUtil;
import com.ticket.sass.provider.config.AppConfig;
import com.ticket.sass.provider.util.EscapeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ywb
 * @date 2022-02-09 14:27
 */
@Data
@ApiModel("详细信息键值对")
public class KeyInfoModel {
    @ApiModelProperty("键")
    private String key;
    @ApiModelProperty("url")
    private String url;
    
    public KeyInfoModel() {
    
    }
    
    public KeyInfoModel(String key) {
        this.key = key;
        this.url = EscapeUtil.prefixStr(key, AppConfig.KEY_PREFIX);
    }

    public static List<KeyInfoModel> generationList(Object cover){
        if (Objects.isNull(cover)){
            return new ArrayList<>();
        }
        
        String input = cover.toString();
        input = input.replaceAll("\\\\\"", "\""); // 替换成双引号
        input = input.replaceAll("\"", ""); // 去掉双引号
        String trimmedInput = input.substring(1, input.length() - 1); // Remove brackets
    
        String[] keyArr = trimmedInput.split(",");
    
        return Arrays.stream(keyArr)
            .filter(StrUtil:: isNotEmpty)
            .map(KeyInfoModel :: new)
            .collect(Collectors.toList());
    }
    
    public static List<KeyInfoModel> generationSingle(Object cover){
        if (Objects.isNull(cover)){
            return new ArrayList<>();
        }
        String input = cover.toString();
        input = input.replaceAll("\\\\\"", "\""); // 替换成双引号
        input = input.replaceAll("\"", ""); // 去掉双引号
        
        List<KeyInfoModel> list = new ArrayList<>();
        list.add(new KeyInfoModel(input));
        return list;
    }
}
